#pragma once

#include "../config.h"

//typedef void (Gateway::*func)(NetPacket& packet);
//typedef map<uint16, func>	 OPCODE_FUNC;
//#define INSTALL_OPCODE_MAP(opcode, FUNC)	(m_opCodeMap.insert(std::make_pair(opcode, FUNC)))
//OPCODE_FUNC m_opCodeMap;

class ServiceManager;

class Gateway : public Connection, public Register
{
	friend class GateServer;
	Gateway(int socket, char* ip, uint16 port);

public:

	enum State
	{
		STATE_NONE = 0,
		STATE_RUN,
		STATE_CLOSING_SERVICE,
		STATE_CLOSED
	};

	virtual ~Gateway();

	virtual int Update();
	virtual void ClosePacket();
	virtual void HandleSendSocket(){ SendPacket(); }
	virtual void HandleRecvSocket(){ RecvPacket(); }

	bool SendPacket();
	void ToNetPacket();
	void RecvPacket();

	bool SendSelectPacket();
	void RecvSelectPacket();
	
	int HandleNetPacket();
	int ExecutePacket(NetPacket& packet);

	void PushSendPacket(NetPacket& packet, BlockTree* tree);

public:

	inline void SetState(State state){ m_state = state; }
	
	inline bool GetInServerSignal()	 { return m_bIsAliveInServer; }

private:

	char*	m_ip;
	uint16  m_port;
	State	m_state;

	ServiceManager* m_serviceManager;

	bool    m_bIsAliveInServer;
};